package com.wze.wze.Util;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

public class OSSUtil {
    String UrlHead = "http://rmauqxfag.hn-bkt.clouddn.com/640.jfif";//链接头，自定义域名
    String UrlHead1 = "http://rmauqxfag.hn-bkt.clouddn.com/";
    String AccessKey ="F0PtUJId7B01kA875r9MtesKrA95cPHgidOLCwGx";
    String Secret="y-3kffOHWiGmUopq4Im_2lEyoJ8J8zYu4rYHEhdZ";
    String BucketName="20020409";

    /**
     * 文件上传
     *
     * @param FilePath 文件路径
     * @param FileName 文件名称
     * @return boolean 是否上传成功
     */
    public String updateFile(String FilePath, String FileName) {
        boolean flag = false;
        String file = null;
        //华北是zone1, [华东z0] [华南z2] [北美na0] [东南亚as0]
        try {
            com.qiniu.storage.Configuration configuration = new com.qiniu.storage.Configuration(Zone.zone2());
            UploadManager uploadManager = new UploadManager(configuration);//七牛云上传管理
            Auth auth = Auth.create(AccessKey,Secret);//传入Key和Secret
            String token = auth.uploadToken(BucketName);//需要传入OSS桶的名称
            Response response = uploadManager.put(FilePath, FileName, token);//上传对象
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class);//将处理结果转化为JSON
            System.out.println(putRet.key);//输出返回值结果
            System.out.println(UrlHead+FileName);//输出OSS地址
            file = UrlHead1+FileName;
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }

    public String updateMFile(MultipartFile multipartFile, String FileName) {
        String file = null;
        //华北是zone1, [华东z0] [华南z2] [北美na0] [东南亚as0]
            try {
                com.qiniu.storage.Configuration configuration = new com.qiniu.storage.Configuration(Zone.zone2());//七牛云配置
                UploadManager uploadManager = new UploadManager(configuration);//七牛云上传管理
                Auth auth = Auth.create("F0PtUJId7B01kA875r9MtesKrA95cPHgidOLCwGx", "y-3kffOHWiGmUopq4Im_2lEyoJ8J8zYu4rYHEhdZ");//需要传入key和secret
                String token = auth.uploadToken("2020080539");//需要传入oss桶名称、

                int dotPos = multipartFile.getOriginalFilename().lastIndexOf(".");
                if (dotPos < 0) {
                    return null;
                }
                String fileExt = multipartFile.getOriginalFilename().substring(dotPos + 1).toLowerCase();
                String fileName = FileName+ "." + fileExt;

                Response res = uploadManager.put(multipartFile.getBytes(), fileName, token);//上传对象
                DefaultPutRet putRet = new Gson().fromJson(res.bodyString(), DefaultPutRet.class);//将处理结果转换为JSON
                String key = putRet.key;
//            System.out.println("http://refky7inl.hb-bkt.clouddn.com/" + JSONObject.parseObject(res.bodyString()).get("key"));
//            return "http://refky7inl.hb-bkt.clouddn.com/"+ JSONObject.parseObject(res.bodyString()).get("key");
                System.out.println(key);
                file = "http://rkp80fsra.hn-bkt.clouddn.com/"+key;
                System.out.println(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return file;
    }

    public String downLoadUrl(String FileName){
        String Url="";
        try{
            String tempUrl=UrlHead+FileName;//临时下载地址
            Auth auth = Auth.create(AccessKey,Secret);//传入Key和Secret
            long time=3600;//链接的有效时间 单位s
            Url=auth.privateDownloadUrl(tempUrl,time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Url;
    }
}
